package com.pkx.cloud.fse7.dms.monitoring.controller;

import com.alibaba.fastjson.JSONObject;
import com.pkx.cloud.fse7.dms.deviceread.entity.DeviceRead;
import com.pkx.cloud.fse7.dms.deviceread.service.DeviceReadService;
import com.pkx.cloud.fse7.dms.monitoring.entity.RemoteOrder;
import com.pkx.cloud.fse7.dms.monitoring.feign.RemoteOrderFeign;
import com.pkx.cloud.fse7.dms.monitoring.service.RemoteOrderService;
import com.pkx.cloud.fse7.expand.group.entity.DoorGroupDetail;
import com.pkx.cloud.fse7.expand.group.service.DoorGroupDetailService;
import com.pkx.cloud.whzx.comn.ORpose;
import com.pkx.cloud.whzx.comn.OutRpose;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author chenwg
 * @date 2020.10.15 12:00
 */
@RestController
@RequestMapping("/dms")
@ORpose
@Api(value = "远程", tags = {"远程"})
public class RemoteOrderController {
    @Resource
    private RemoteOrderFeign remoteOrderFeign;
    @Resource
    private RemoteOrderService remoteOrderService;
    @Resource
    private DeviceReadService readService;
    @Resource
    private DoorGroupDetailService doorGroupDetailService;

    /**
     * 远程开门
     *
     * @param remoteOrder
     * @return
     */
    @PostMapping("/Monitoring/RemoteOrder")
    @ApiOperation("远程操作")
    public OutRpose open(@RequestBody RemoteOrder remoteOrder) {
        remoteOrder.setSubSystem(2);
        remoteOrder.setOrderDate(new Date());
        remoteOrder.setIsLegal(true);
        remoteOrder.setOrder(1);
        remoteOrder.setIsLogHandGate(true);
        remoteOrderService.open(remoteOrder);
        return OutRpose.Ok("指令发送成功");
    }

    /**
     * 远程关门
     *
     * @param remoteOrder
     * @return
     */
    @PostMapping("/Monitoring/close")
    @ApiOperation("远程关门")
    public OutRpose close(@RequestBody RemoteOrder remoteOrder) {
        remoteOrder.setSubSystem(2);
        remoteOrder.setOrderDate(new Date());
        remoteOrder.setIsLegal(true);
        remoteOrder.setOrder(2);
        remoteOrder.setIsLogHandGate(true);
        remoteOrderService.open(remoteOrder);
        return OutRpose.Ok("指令发送成功");
    }

    /**
     * 远程锁门
     *
     * @param remoteOrder
     * @return
     */
    @PostMapping("/Monitoring/lock")
    @ApiOperation("远程锁门")
    public OutRpose lock(@RequestBody RemoteOrder remoteOrder) {
        remoteOrder.setSubSystem(2);
        remoteOrder.setOrderDate(new Date());
        remoteOrder.setIsLegal(true);
        remoteOrder.setOrder(3);
        remoteOrder.setIsLogHandGate(true);
        remoteOrderService.open(remoteOrder);
        return OutRpose.Ok("指令发送成功");
    }

    /**
     * 常开
     *
     * @param remoteOrder
     * @return
     */
    @PostMapping("/Monitoring/normalOpen")
    @ApiOperation("常开")
    public OutRpose normalOpen(@RequestBody RemoteOrder remoteOrder) {
        remoteOrder.setSubSystem(2);
        remoteOrder.setOrderDate(new Date());
        remoteOrder.setIsLegal(true);
        remoteOrder.setOrder(5);
        remoteOrder.setIsLogHandGate(true);
        String open = remoteOrderService.open(remoteOrder);
        return OutRpose.Ok("指令发送成功");
    }



    /**
     * 常闭
     *
     * @param remoteOrder
     * @return
     */
    @PostMapping("/Monitoring/normalClose")
    @ApiOperation("常闭")
    public OutRpose normalClose(@RequestBody RemoteOrder remoteOrder) {
        remoteOrder.setSubSystem(2);
        remoteOrder.setOrderDate(new Date());
        remoteOrder.setIsLegal(true);
        remoteOrder.setOrder(6);
        remoteOrder.setIsLogHandGate(true);
        String open = remoteOrderService.open(remoteOrder);
        return OutRpose.Ok("指令发送成功");
    }

    /**
     * 正常
     *
     * @param remoteOrder
     * @return
     */
    @PostMapping("/Monitoring/normal")
    @ApiOperation("正常")
    public OutRpose normal(@RequestBody RemoteOrder remoteOrder) {
        remoteOrder.setSubSystem(2);
        remoteOrder.setOrderDate(new Date());
        remoteOrder.setIsLegal(true);
        remoteOrder.setOrder(7);
        remoteOrder.setIsLogHandGate(true);
        remoteOrderService.open(remoteOrder);
        return OutRpose.Ok("指令发送成功");
    }

    @GetMapping("/openGroup/{planId}")
    @ApiOperation("计划 远程操作")
    public OutRpose open(@PathVariable(value = "planId") String planId) {
        List<DoorGroupDetail> allByPlanId = doorGroupDetailService.getAllByPlanId(planId);
        allByPlanId.forEach(deviceRead -> {
            RemoteOrder remoteOrder = new RemoteOrder();
            remoteOrder.setSubSystem(2);
            remoteOrder.setOrderDate(new Date());
            remoteOrder.setIsLegal(true);
            remoteOrder.setIsLogHandGate(true);
            remoteOrder.setOrder(1);
            remoteOrder.setDevId(deviceRead.getDevId());
            remoteOrder.setParameter(String.valueOf(deviceRead.getDoorNo()));
            remoteOrderService.open(remoteOrder);
        });
        return OutRpose.Ok("指令发送成功");
    }

    @GetMapping("/batch/openGroup/{planIds}")
    @ApiOperation("计划批量远程操作")
    public OutRpose batchGroupOpen(@PathVariable(value = "planIds") List<String> planIds) {
        List<DoorGroupDetail> allByPlanId = doorGroupDetailService.getAllByPlanIds(planIds);
        allByPlanId.forEach(deviceRead -> {
            RemoteOrder remoteOrder = new RemoteOrder();
            remoteOrder.setSubSystem(2);
            remoteOrder.setOrderDate(new Date());
            remoteOrder.setIsLegal(true);
            remoteOrder.setIsLogHandGate(true);
            remoteOrder.setOrder(1);
            remoteOrder.setDevId(deviceRead.getDevId());
            remoteOrder.setParameter(String.valueOf(deviceRead.getDoorNo()));
            remoteOrderService.open(remoteOrder);
        });
        return OutRpose.Ok("指令发送成功");
    }

    @GetMapping("/Monitoring/oneKey")
    @ApiOperation("一键开门")
    public OutRpose oneKey() {
        List<DeviceRead> deviceReads = readService.getAll();
        deviceReads.forEach(deviceRead -> {
            RemoteOrder remoteOrder = new RemoteOrder();
            remoteOrder.setSubSystem(2);
            remoteOrder.setOrderDate(new Date());
            remoteOrder.setIsLegal(true);
            remoteOrder.setIsLogHandGate(true);
            remoteOrder.setOrder(1);
            remoteOrder.setDevId(deviceRead.getRid());
            remoteOrder.setParameter(String.valueOf(deviceRead.getDoorNo()));
            remoteOrderService.open(remoteOrder);
        });
        return OutRpose.Ok("指令发送成功");
    }

    @PostMapping("/Monitoring/batch")
    @ApiOperation("远程操作")
    public OutRpose batch(@RequestBody List<RemoteOrder> list) {
        list.forEach(li -> {
            RemoteOrder remoteOrder = new RemoteOrder();
            remoteOrder.setSubSystem(2);
            remoteOrder.setOrderDate(new Date());
            remoteOrder.setIsLegal(true);
            remoteOrder.setIsLogHandGate(true);
            remoteOrder.setOrder(1);
            remoteOrder.setDevId(li.getDevId());
            remoteOrder.setParameter(li.getParameter());
            remoteOrderService.open(remoteOrder);
        });
        return OutRpose.Ok("指令发送成功");
    }

    @PostMapping("/Monitoring/batchNormal")
    @ApiOperation("批量正常")
    public OutRpose batchNormal(@RequestBody List<RemoteOrder> list) {
        list.forEach(li -> {
            RemoteOrder remoteOrder = new RemoteOrder();
            remoteOrder.setSubSystem(2);
            remoteOrder.setOrderDate(new Date());
            remoteOrder.setIsLegal(true);
            remoteOrder.setIsLogHandGate(true);
            remoteOrder.setOrder(7);
            remoteOrder.setDevId(li.getDevId());
            remoteOrder.setParameter(li.getParameter());
            remoteOrderService.open(remoteOrder);
        });
        return OutRpose.Ok("指令发送成功");
    }

    @PostMapping("/Monitoring/batchNormalOpen")
    @ApiOperation("批量常开")
    public OutRpose batchNormalOpen(@RequestBody List<RemoteOrder> list) {
        list.forEach(li -> {
            RemoteOrder remoteOrder = new RemoteOrder();
            remoteOrder.setSubSystem(2);
            remoteOrder.setOrderDate(new Date());
            remoteOrder.setIsLegal(true);
            remoteOrder.setIsLogHandGate(true);
            remoteOrder.setOrder(5);
            remoteOrder.setDevId(li.getDevId());
            remoteOrder.setParameter(li.getParameter());
            remoteOrderService.open(remoteOrder);
        });
        return OutRpose.Ok("指令发送成功");
    }

    @PostMapping("/Monitoring/batchNormalClose")
    @ApiOperation("批量常闭")
    public OutRpose batchNormalClose(@RequestBody List<RemoteOrder> list) {
        list.forEach(li -> {
            RemoteOrder remoteOrder = new RemoteOrder();
            remoteOrder.setSubSystem(2);
            remoteOrder.setOrderDate(new Date());
            remoteOrder.setIsLegal(true);
            remoteOrder.setIsLogHandGate(true);
            remoteOrder.setOrder(6);
            remoteOrder.setDevId(li.getDevId());
            remoteOrder.setParameter(li.getParameter());
            remoteOrderService.open(remoteOrder);
        });
        return OutRpose.Ok("指令发送成功");
    }


    @PostMapping("/Monitoring/BatchRemoteOrder")
    @ApiOperation("批量远程操作")
    public OutRpose open(@RequestBody List<RemoteOrder> list) {
        list.forEach(li -> {
            RemoteOrder remoteOrder = new RemoteOrder();
            remoteOrder.setSubSystem(2);
            remoteOrder.setOrderDate(new Date());
            remoteOrder.setIsLegal(true);
            remoteOrder.setIsLogHandGate(true);
            remoteOrder.setOrder(1);
            remoteOrder.setDevId(li.getDevId());
            remoteOrder.setParameter(li.getParameter());
            remoteOrderService.open(remoteOrder);
        });
        return OutRpose.Ok("指令发送成功");
    }

    /**
     * 推送数据查询接口
     *
     * @param jsonObject
     * @return
     */
    @GetMapping("OperationMark/GetByFunc")
    public String getByFunc(@RequestBody JSONObject jsonObject) {
        return remoteOrderFeign.getByFunc(jsonObject);
    }


    /**
     * 获取系统监控状态
     *
     * @return
     */
    @PostMapping("MonitorStatus/GetByCustom")
    public String getByCustom(@RequestBody JSONObject str) {
        return remoteOrderFeign.getByCustom(str);
    }

}
